package com.fau.mm.security;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

@Service
public class MyAccessDecisionManager implements AccessDecisionManager {

	@Override
	public void decide(Authentication arg0, Object arg1,
			Collection<ConfigAttribute> arg2) throws AccessDeniedException,
			InsufficientAuthenticationException {
		if(null == arg2) return;
		Iterator<ConfigAttribute> iterator = arg2.iterator();
		while(iterator.hasNext()){
			ConfigAttribute configAttribute = iterator.next();
			for(GrantedAuthority ga : arg0.getAuthorities()){
				if(configAttribute.getAttribute().equals(ga.getAuthority())) return;
			}
		}
		throw new AccessDeniedException("没有权限访问");
	}

	@Override
	public boolean supports(ConfigAttribute arg0) {
		return true;
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}
	
}
